﻿using Alimentador.Foods;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Alimentador.BottomGroup.Results
{
    public class VitaminValue
    {
        private double m_quantity = 0.00;
        private string m_name = "";
        
        /// <summary>
        /// Constructor with default values
        /// </summary>
        /// <param name="name">string</param>
        /// <param name="quantity">double</param>
        public VitaminValue(string name = "", double quantity = 0.00)
        {
            m_name = name;
            m_quantity = quantity;
        }

        public string GetPropertyName
        {
            get
            {
                return "";
                //return m_name;
            }
            set
            {
                m_name = value;
            }
        }

        public double Quantity
        {
            get
            {
                return System.Math.Round(m_quantity, 2);
            }
            set
            {
                m_quantity = value;
            }
        }
    }


    /// <summary>
    /// Interaction logic for VitaminsPieChart.xaml
    /// </summary>
    public partial class VitaminsColumnChart : UserControl
    {
        public Dictionary<PropertyName, string> m_vitamins = new Dictionary<PropertyName, string>()
        {
            {PropertyName.VitaminaATotal,   "Vit. A"},
            {PropertyName.VitaminaC,        "Vit. C"},
            {PropertyName.VitaminaB6,       "Vit. B6"},
            {PropertyName.VitaminaB12,      "Vit. B12"},
            {PropertyName.ATocoferol,       "ATocoferol"},
            {PropertyName.Folatos,          "Folatos"},
            {PropertyName.VitaminaD,        "Vit. D"},
            {PropertyName.Tiamina,          "Tiamina"},
            {PropertyName.Riboflavina,      "Riboflavina"},
            {PropertyName.Niacina,          "Niacina"}
        };

        private List<FoodBase> m_foodList = new List<FoodBase>();
        private int m_numDays = 1;
        private static bool m_IsShownInPopUp;

        /// <summary>
        /// Constructor
        /// </summary>
        public VitaminsColumnChart()
        {
            InitializeComponent();

            ColumnChartAxis.Maximum = 200;
            ColumnChartAxis.Minimum = 0;

            PopulateChart();
            RefreshChart();
        }

        /// <summary>
        /// Fill the chart with default values
        /// </summary>
        private void PopulateChart()
        {
            ColumnChart.FontSize = 10;

            string str = m_vitamins[PropertyName.VitaminaATotal];
            seriesVitA.DataContext = new VitaminValue(str, 0);
            seriesVitA.Title = str;

            str = m_vitamins[PropertyName.VitaminaC];
            seriesVitC.DataContext = new VitaminValue(str, 0);
            seriesVitC.Title = str;

            str = m_vitamins[PropertyName.VitaminaB6];
            seriesVitB6.DataContext = new VitaminValue(str, 0);
            seriesVitB6.Title = str;

            str = m_vitamins[PropertyName.VitaminaB12];
            seriesVitB12.DataContext = new VitaminValue(str, 0);
            seriesVitB12.Title = str;

            str = m_vitamins[PropertyName.ATocoferol];
            seriesAtocoferol.DataContext = new VitaminValue(str, 0);
            seriesAtocoferol.Title = str;

            str = m_vitamins[PropertyName.Folatos];
            seriesFolatos.DataContext = new VitaminValue(str, 0);
            seriesFolatos.Title = str;

            str = m_vitamins[PropertyName.VitaminaD];
            seriesVitD.DataContext = new VitaminValue(str, 0);
            seriesVitD.Title = str;

            str = m_vitamins[PropertyName.Tiamina];
            seriesTiamina.DataContext = new VitaminValue(str, 0);
            seriesTiamina.Title = str;

            str = m_vitamins[PropertyName.Niacina];
            seriesNiacina.DataContext = new VitaminValue(str, 0);
            seriesNiacina.Title = str;

            str = m_vitamins[PropertyName.Riboflavina];
            seriesRiboflavina.DataContext = new VitaminValue(str, 0);
            seriesRiboflavina.Title = str;
        }

        /// <summary>
        /// Refresh chart values
        /// </summary>
        public void RefreshChart()
        {
            if (MainWindow.CurrentPatient == null)
            {
                return;
            }

            double max = 100;
            double val = FoodFormulas.PercentageVitaminA(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesVitA.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.VitaminaATotal], val) };
            seriesVitA.Title = m_vitamins[PropertyName.VitaminaATotal] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageVitaminC(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesVitC.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.VitaminaC], val) };
            seriesVitC.Title = m_vitamins[PropertyName.VitaminaC] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageVitaminB6(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesVitB6.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.VitaminaB6], val) };
            seriesVitB6.Title = m_vitamins[PropertyName.VitaminaB6] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageVitaminB12(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesVitB12.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.VitaminaB12], val) };
            seriesVitB12.Title = m_vitamins[PropertyName.VitaminaB12] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageATocoferol(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesAtocoferol.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.ATocoferol], val) };
            seriesAtocoferol.Title = m_vitamins[PropertyName.ATocoferol] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageFolatos(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesFolatos.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.Folatos], val) };
            seriesFolatos.Title = m_vitamins[PropertyName.Folatos] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageVitaminD(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesVitD.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.VitaminaD], val) };
            seriesVitD.Title = m_vitamins[PropertyName.VitaminaD] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageTiamina(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesTiamina.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.Tiamina], val) };
            seriesTiamina.Title = m_vitamins[PropertyName.Tiamina] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageNiacina(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesNiacina.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.Niacina], val) };
            seriesNiacina.Title = m_vitamins[PropertyName.Niacina] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            val = FoodFormulas.PercentageRiboflavina(m_foodList, MainWindow.CurrentPatient, m_numDays);
            seriesRiboflavina.DataContext = new VitaminValue[] { new VitaminValue(m_vitamins[PropertyName.Riboflavina], val) };
            seriesRiboflavina.Title = m_vitamins[PropertyName.Riboflavina] + ": " + val.ToString("F2") + "%";
            max = (val > max) ? val + 10 : max;

            ColumnChartAxis.Maximum = max;
            ColumnChart.Refresh();
        }

        /// <summary>
        /// Hanlde the event for adding a Food to list
        /// </summary>
        /// <param name="foodList">List(Food)</param>
        /// <param name="numDays">int</param>
        internal void AddFood(List<FoodBase> foodList, int numDays = 1)
        {
            m_foodList = foodList;
            m_numDays = numDays;
            RefreshChart();
        }

        private void pieChart_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!m_IsShownInPopUp)
            {
                TheMainGrid.Children.Remove(ColumnChart);

                ChartPopUp cpp = new ChartPopUp(ColumnChart);
                cpp.Owner = Application.Current.MainWindow;
                m_IsShownInPopUp = true;
                cpp.ShowDialog();

                ColumnChart = cpp.getChartBack();

                TheMainGrid.Children.Add(ColumnChart);
                m_IsShownInPopUp = false;
            }
        }
    }
}
